package org.projects.graduates.domain;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.projects.graduates.Constants;
import org.projects.graduates.utils.EncryptionTool;

import com.dayatang.domain.AbstractEntity;
import com.dayatang.domain.QuerySettings;

@Entity
@Table(name = "users_")
public class User extends AbstractEntity {

	private static final long serialVersionUID = -5078861910742827230L;
	
	public final static String S_PORTRAIT = "  ";

	private String username;
	private String password;

	public User() {
		super();
	}

	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	public static User userAfterEncryption(String username, String sourcePassword){
		return new User(username, EncryptionTool.convert(sourcePassword, Constants.SECURITY_TYPE));
	}

	public static User findby(String username, String password) {
		QuerySettings<User> settings = QuerySettings.create(User.class);
		settings.eq("username", username).eq("password", password);
		return getRepository().getSingleResult(settings);
	}

	public static User findby(String username, char[] password) {
		String psw = String.valueOf(password);
		return findby(username, psw);
	}

	public static User findby(String username) {
		QuerySettings<User> settings = QuerySettings.create(User.class);
		settings.eq("username", username);
		return getRepository().getSingleResult(settings);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof User) {
			return ((User) obj).getId().equals(this.getId());
		}

		return false;
	}

	@Override
	public int hashCode() {
		return username.hashCode();
	}

	public String toString() {
		return getUsername();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
